package com.learning.controller.student;

import com.learning.constant.JwtClaimsConstant;
import com.learning.dto.LoginDTO;
import com.learning.entity.Student;
import com.learning.properties.JwtProperties;
import com.learning.result.Result;
import com.learning.service.IStudentService;
import com.learning.utils.JwtUtil;
import com.learning.vo.LoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController("studentStudentController")
@RequestMapping("/student")
@Slf4j
@RequiredArgsConstructor
@Api(tags = "学生相关接口")
public class StudentController {

	private final IStudentService studentService;
	private final JwtProperties jwtProperties;

	@ApiOperation("学生登录")
	@PostMapping("/login")
	public Result<LoginVO> login(@RequestBody LoginDTO loginDTO) {
		log.info("学生登录：{}", loginDTO);
		Student student = studentService.login(loginDTO);

		//登录成功后，生成jwt令牌
		Map<String, Object> claims = new HashMap<>();
		claims.put(JwtClaimsConstant.STUDENT_ID, student.getId());
		String token = JwtUtil.createJWT(
				jwtProperties.getStudentSecretKey(),
				jwtProperties.getStudentTtl(),
				claims);

		LoginVO loginVO = LoginVO.builder()
				.id(student.getId())
				.userName(student.getUsername())
				.token(token)
				.build();

		return Result.success(loginVO);
	}

	@ApiOperation("学生退出")
	@PostMapping("/logout")
	public Result<String> logout() {
		return Result.success();
	}

}
